package net.awpspace.walkman.view;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

import net.awpspace.walkman.entity.MP3;
import net.awpspace.walkman.utils.BackStack;
import net.awpspace.walkman.utils.Engine;
import net.awpspace.walkman.utils.ImageUtils;
import net.awpspace.walkman.utils.Loader;

/**
 * View: Now playing
 * 
 * @author Diep AwpSpace
 * 
 */
public class Playing extends GameCanvas {

	public static MP3 track;
	public static int index;
	public static int volume;

	private Engine engine;

	private Sprite ctrlPrev;
	private Sprite ctrlPlay;
	private Sprite ctrlNext;

	private Sprite sSelected;

	private boolean playing;

	public Playing(boolean arg0) {
		super(arg0);
		setFullScreenMode(true);
		volume = 10;
		engine = new Engine();
		ctrlPrev = new Sprite(ImageUtils.getImage(ImageUtils.CTRL_PREV), 72, 72);
		ctrlPrev.setPosition(0, 310);
		ctrlNext = new Sprite(ImageUtils.getImage(ImageUtils.CTRL_NEXT), 72, 72);
		ctrlNext.setPosition(168, 310);
		ctrlPlay = new Sprite(ImageUtils.getImage(ImageUtils.CTRL_PLAY), 72, 72);
		ctrlPlay.setPosition(84, 310);
		playing = false;
	}

	/**
	 * Custom drawing
	 */
	public void paint(Graphics g) {
		g.setColor(0x12816b);
		g.fillRect(0, 0, 240, 400);
		g.drawImage(ImageUtils.getImage(ImageUtils.IMG_HEADER), 0, 0, 0);
		g.drawImage(ImageUtils.getImage(ImageUtils.IMG_BACK), 11, 11, 0);
		g.drawImage(ImageUtils.getImage(ImageUtils.IMG_ARTIST), 18, 210, 0);
		g.drawImage(ImageUtils.getImage(ImageUtils.IMG_ALBUM), 18, 245, 0);
		g.drawImage(ImageUtils.getImage(ImageUtils.IMG_TRACK), 18, 280, 0);
		g.drawImage(ImageUtils.getImage(ImageUtils.IMG_ART), 24, 64, 0);

		g.setColor(0xffffff);
		g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN,
				Font.SIZE_MEDIUM));
		if (track == null) {
			track = (MP3) Loader.musics.elementAt(0);
			index = 0;
		}
		g.drawString(track.artist == null ? "Unknown" : track.artist, 45, 224,
				Graphics.LEFT | Graphics.BASELINE);
		g.drawString(track.album == null ? "Unknown" : track.album, 45, 260,
				Graphics.LEFT | Graphics.BASELINE);
		g.drawString(track.title == null ? "Unknown" : track.title, 45, 294,
				Graphics.LEFT | Graphics.BASELINE);

		ctrlPrev.paint(g);
		ctrlPlay.paint(g);
		ctrlNext.paint(g);

		flushGraphics();
	}

	/**
	 * onClick method handler
	 */
	protected void pointerPressed(int x, int y) {
		sSelected = getSelectedSprite(x, y);
		if (sSelected != null) {
			if (sSelected.equals(ctrlPlay)) {
				if (playing) {
					sSelected.setFrame(3);
				} else {
					sSelected.setFrame(1);
				}
			} else {
				sSelected.setFrame(1);
			}
		}
		repaint();
	}

	/**
	 * UI changing handler
	 */
	protected void pointerReleased(int x, int y) {
		if (x > 11 && x < 36 && y > 11 && y < 36) {
			BackStack.getInstance(null).back();
			return;
		}
		if (sSelected != null) {
			if (sSelected.equals(ctrlPlay)) {
				if (playing) {
					sSelected.setFrame(2);
				} else {
					sSelected.setFrame(0);
				}
			} else {
				sSelected.setFrame(0);
			}
		}
		Sprite tmp = getSelectedSprite(x, y);
		if (tmp != null && tmp.equals(sSelected)) {
			if (sSelected.equals(ctrlPrev)) {
				if (index == 0) {
					index = Loader.musics.size() - 1;
				} else {
					index--;
				}
				engine.stopNoise();
				track = (MP3) Loader.musics.elementAt(index);
				new Thread() {
					
					public void run() {
						engine.readNoise(track);
						engine.playNoise();
						playing = true;
					}
				}.start();
			} else if (sSelected.equals(ctrlNext)) {
				if (index == Loader.musics.size() - 1) {
					index = 0;
				} else {
					index++;
				}
				engine.stopNoise();
				track = (MP3) Loader.musics.elementAt(index);
				new Thread() {
					
					public void run() {
						engine.readNoise(track);
						engine.playNoise();
						playing = true;
					}
				}.start();
			} else if (sSelected.equals(ctrlPlay)) {
				if (playing) {
					engine.pauseNoise();
					playing = false;
					sSelected.setFrame(0);
				} else {
					new Thread() {
						
						public void run() {
							engine.readNoise(track);
							engine.playNoise();
							playing = true;
						}
					}.start();
					sSelected.setFrame(2);
				}
			}
		}
		repaint();
	}

	/**
	 * Get selected sprite from coordinate
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	private Sprite getSelectedSprite(int x, int y) {
		if (y > 310 && y < 382) {
			if (x < 72) {
				return ctrlPrev;
			}
			if (x > 84 && x < 156) {
				return ctrlPlay;
			}
			if (x > 168) {
				return ctrlNext;
			}
		}
		return null;
	}
}
